package com.example.clientecine;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.JsonObject;

public class UpdateSala extends Activity {

	private String URL = "http://10.0.2.2:8080/ServicioWebSala/services/CineORMWS";
	private String NAMESPACE = "http://ws";

	Sala sala;
	EditText nombreEditText;
	EditText butacasEditText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_sala);
		nombreEditText = (EditText) findViewById(R.id.addsala_nombre);
		butacasEditText = (EditText) findViewById(R.id.addsala_butacas);

	}

	public void guardar(View v) {
		JsonObject jsonObjectPelicula = new JsonObject();
		// jsonObjectPelicula.addProperty("nombre", "peli");
		// jsonObjectPelicula.addProperty("descripcion", "peliculaaaas");
		// jsonObjectPelicula.addProperty("estreno", 1);
		// jsonObjectPelicula.addProperty("genero_id", "1");
		// jsonObjectPelicula.addProperty("clasificacion_id", "1");

		jsonObjectPelicula.addProperty("id", sala.getId());
		jsonObjectPelicula.addProperty("nombre", nombreEditText.getText()
				.toString());
		jsonObjectPelicula.addProperty("descripcion", butacasEditText.getText()
				.toString());
		Log.d("", jsonObjectPelicula.toString());
		new UpdatePeliculaBackground().execute(jsonObjectPelicula.toString());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_update_sala, menu);
		return true;
	}

	class UpdatePeliculaBackground extends AsyncTask<String, Void, String> {

		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(UpdateSala.this, "Cine",
					"Espere mientras se actualizando la sala...", true);
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {

			String resultado = new String();
			String METHOD_NAME = "updateSala";
			String SOAP_ACTION = NAMESPACE + "/" + METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			request.addProperty("jsonSala", params[0]);

			HttpTransportSE aht = new HttpTransportSE(URL);

			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
						.getResponse();
				resultado = resultString.toString();

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultado;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			// Valido que tenga la informacion necesaria
			if ("1".equals(result)) {
				Toast.makeText(UpdateSala.this, "Ingresada correctamente",
						Toast.LENGTH_LONG).show();
				finish();
			} else {
				Toast.makeText(UpdateSala.this,
						"Error al obtener la informacion necesaria",
						Toast.LENGTH_LONG).show();
			}
		}
	}

	class RemovePeliculaBackground extends AsyncTask<Integer, Void, String> {

		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(UpdateSala.this,
					"Cartelera de Cine",
					"Espere mientras se elimina la pelicula...", true);
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Integer... params) {

			String resultado = new String();
			String METHOD_NAME = "removePelicula";
			String SOAP_ACTION = NAMESPACE + "/" + METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			request.addProperty("peliculaId", params[0]);

			HttpTransportSE aht = new HttpTransportSE(URL);

			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
						.getResponse();
				resultado = resultString.toString();

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultado;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			// Valido que tenga la informacion necesaria
			if ("1".equals(result)) {
				Toast.makeText(UpdateSala.this, "Eliminada correctamente",
						Toast.LENGTH_LONG).show();
				finish();
			} else {
				Toast.makeText(UpdateSala.this,
						"Error al obtener la informacion necesaria",
						Toast.LENGTH_LONG).show();
			}
		}
	}
}
